var util = require("../../utils/util.js")
var Bmob = require('../../utils/Bmob-1.7.1.min.js');
// console.log(Bmob)
Bmob.initialize("9e5125bcb5f001196509263c4daa9c79", "a59531bc1370b741dfd9177b9f525e09");
var startPoint;
Page({

  data: {
    // 悬浮按钮初始top let height width
    buttonTop: 400,
    buttonLeft: 220,
    windowHeight: '',
    windowWidth: '',

    // 获取年月日
    month: "",
    day: "",
    hours: "",
    minutes: "",

    // list
    swList: [],
    srList: [],

    // swiper滑动下标
    currentTab: 0,

    // 页面总高度将会放在这里
    // navbar的高度
    navbarHeight: 0,
    // header的高度
    headerHeight: 0,
    // scroll-view的高度
    scrollViewHeight: 0,
    //  日常事务 index
    index: 0,
    // item2 birthday列表长按获取下标
    bIndex: 0,
    // bIndex对应bomb的objectId
    bmobIndex: '',

    // 纪念日模块
    currentIndex: 0,
    // 设置进入cd页面的index
    flag:0,
    crdList: [],
    currentIndexs: 0,
    cIndex: 0,
    intoIndex:0



  },

  onLoad: function(options) {
    console.log("dsafsadfsaf =" + this.data.crdList)
    wx.setNavigationBarTitle({
      title: '日常事务',
    })
    // 悬浮按钮部分
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        // console.log(res);
        // 屏幕宽度、高度
        // console.log('height=' + res.windowHeight);
        // console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,

        })
      }
    })
    let query = wx.createSelectorQuery().in(this);
    // 然后逐个取出navbar和header的节点信息
    // 选择器的语法与jQuery语法相同
    // query.select('#tabbar').boundingClientRect();
    query.select('#header').boundingClientRect();


    // 执行上面所指定的请求，结果会按照顺序存放于一个数组中，在callback的第一个参数中返回
    query.exec((res) => {
      // console.log(res)
      // 分别取出navbar和header的高度
      // let navbarHeight = res[0].height;
      let headerHeight = res[0].dataset.height

      // 然后就是做个减法
      let scrollViewHeight = this.data.windowHeight - headerHeight - 100;

      // 算出来之后存到data对象里面
      this.setData({
        scrollViewHeight: scrollViewHeight
      });
    });


  },

  // swiper点击事件处理
  swichNav: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
    // 关闭dialog
    this.dialog.close();
    this.dialogs.close();
    this.dialogss.close();

  },
  /**
   * 导航页面显示
   * swiper 滑动事件处理
   */
  swiperChange: function(e) {
    // 关闭dialog
    this.dialog.close();
    this.dialogs.close();
    this.dialogss.close();
    this.setData({
      currentTab: e.detail.current,
    })

  },

  // 页面初始化数据
  onShow: function() {
    // ["你", 2019, 11, 24, "深夜", 21, "38"]
    // 从bmob取数据
    // 事务
    const query = Bmob.Query('UserContent');
    let temp = [];
    query.find().then(res => {
      console.log('ress = ' + res.length)
      for (var i = res.length - 1; i >= 0; i--) {
        temp.push(res[i])
      }
      this.setData({
        swList: temp,
      })
      console.log("temp = " + this.data.swList[0].objectId)
    });
    
// 生日
    const query2 = Bmob.Query('Userbirthday');
    let birdthay = [];
    query2.find().then(res => {
      console.log('res11= ' + res[0].name)
      for (var i = res.length - 1; i >= 0; i--) {
        birdthay.push(res[i])
      }
      this.setData({
        srList: birdthay,
      })
    });

    // 纪念日模块
    const query3 = Bmob.Query('UsercommemorationDay');
    let clist = [];
    query3.find().then(res => {
      console.log('res22= ' + res[0].content)
      for (var i = res.length - 1; i >= 0; i--) {
        clist.push(res[i])
      }
      this.setData({
        crdList: clist,
        currentIndex: this.data.flag,
        // dy:-282


      })
      // if (this.data.flag == this.data.currentIndex){
      //   this.setData({
      //     currentIndex:this.data.flag
      //   })

      // }
      console.log("eeeee= " + this.data.crdList[0].content)
    });
  

  },


  onReady: function() {
    var that = this;
    // 因为层级关系 要用延迟来捕获自定义组件Dialog的id
    that.dialog = that.selectComponent('#dialog');
    that.dialogs = that.selectComponent('#dialogs');
    that.dialogss = that.selectComponent('#dialogss');
    console.log("ttttt = " + that.dialogsss)
    var str = setTimeout(function() {
      that.dialog = that.selectComponent('#dialog');
      that.dialogs = that.selectComponent('#dialogs');
      that.dialogss = that.selectComponent('#dialogss');
      console.log('初始化dialog完毕')
    }, 1000)
 

  },
  // 日常事务长按事件
  deleteItem: function(e) {
    console.log(e)
    // var index = e.currentTarget.dataset.index;
    // console.log(this.data.textareaValueList)
    var that = this;
    that.setData({
      index: e.currentTarget.dataset.index,
    })

    that.dialogss.setContent();
    that.dialogss.show();
  },

  // 取消
  cancelEventss: function() {
    console.log(this.dialogss.data.cancelText);
    this.dialogss.close();
  },
  // 删除全部
  deleteAll: function() {
    var that = this;
    that.dialogss.close();
    wx.showModal({
      title: '提示',
      content: '你确定要删除全部吗',
      success(res) {
        if (res.confirm) {
          that.setData({
            swList: ''
          })
          const query = Bmob.Query('UserContent');
          // query.limit(50)
          query.find().then(todos => {
            todos.destroyAll().then(res => {
              // 成功批量修改
              console.log(res, 'ok')
            }).catch(err => {
              console.log(err)
            });
          })
        } else if (res.cancel) {}
      }
    })

  },
  // 确定·
  okEventss: function() {
    // console.log(this.dialogss.data.okText);
    // console.log(this.data.textareaValueList)
    let object = this.data.swList[this.data.index].objectId;
    let temp = [];
    let list = this.data.swList;
    list.splice(this.data.index, 1);
    this.setData({
      swList: list
    })
    const query = Bmob.Query('UserContent');
    query.destroy(object).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })

    this.dialogss.close();
  },
  // 生日模块点击事件
  xgSrBind(e){
    // e.currentTarget.dataset.index
   
    let temp = this.data.srList[e.currentTarget.dataset.index]
   
    console.log("qwe = "+temp.blist);
    wx.navigateTo({
      // url: '../birthday/birthday?temp = ' + temp.blist,
    })

  },
  // 生日模块长按事件
  tapDialog: function(e) {

    var that = this;
    that.setData({
      bIndex: e.currentTarget.dataset.index,
    })
    that.dialog.setContent();
    that.dialog.show();
  },
  // 取消
  cancelEvent: function() {
    console.log(this.dialog.data.cancelText);
    this.dialog.close();
  },
  // 删除全部
  deleteAllB() {
    var that = this;
    this.dialog.close();
    wx.showModal({
      title: '提示',
      content: '你确定要删除全部吗',
      success(res) {
        if (res.confirm) {
          that.setData({
            srList: ''
          })
          const query = Bmob.Query('Userbirthday');
          // query.limit(50)
          query.find().then(todos => {
            todos.destroyAll().then(res => {
              // 成功批量修改
              console.log(res, 'ok')
            }).catch(err => {
              console.log(err)
            });
          })
        } else if (res.cancel) {}
      }
    })
  },
  // 确定·
  okEvent: function() {
    console.log(this.dialog.data.okText);
    console.log(this.data.srList[this.data.bIndex].objectId)
    let object = this.data.srList[this.data.bIndex].objectId;
    let temp = [];
    let list = this.data.srList;
    list.splice(this.data.bIndex, 1);
    this.setData({
      srList: list
    })
    const query = Bmob.Query('Userbirthday');
    query.destroy(object).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
    this.dialog.close();
  },

  // 纪念日模块
  // picker时间选择器
  bindDateChangeC: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  /* 这里实现控制中间凸显图片的样式 */
  handleChange: function(e) {
    console.log(e.detail.current)
    this.setData({
      currentIndex: e.detail.current

    })
  },
  // 纪念日
  // swiper长按事件
  tapSwiper(e) {
    var that = this;
    // 获取当前下标
    that.setData({
      cIndex: e.currentTarget.dataset.index

    })
    console.log(that.data.cIndex)
    that.dialogs.setContent();
    that.dialogs.show();

  },
  // 取消
  cancelEvents: function() {
    console.log(this.dialogs.data.cancelText);
    this.dialogs.close();
  },
  deleteAllC() {
    var that = this;
    this.dialogs.close();
    wx.showModal({
      title: '提示',
      content: '你确定要删除全部吗',
      success(res) {
        if (res.confirm) {
          that.setData({
            crdList: ''
          })
          const query = Bmob.Query('UsercommemorationDay');
          // query.limit(50)
          query.find().then(todos => {
            todos.destroyAll().then(res => {
              // 成功批量修改
              console.log(res, 'ok')
            }).catch(err => {
              console.log(err)
            });
          })
        } else if (res.cancel) {}
      }
    })
  },
  // 确定·删除事件
  okEvents: function() {
    console.log(this.dialogs.data.okText);
    // console.log(this.data.crdList[this.data.cIndex].objectId)
    // 获取当前swiper item 下标的objectids
    let object = this.data.crdList[this.data.cIndex].objectId;
    let temp = [];
    let list = this.data.crdList;
    // 删除本列表的数据
    list.splice(this.data.cIndex, 1);
    this.setData({
      crdList: list,
      // swiper当前下标设置为0 不然报Cannot read property '$$' of undefined
      currentIndexs: 0
    })
    // bmob删除数据
    const query = Bmob.Query('UsercommemorationDay');
    query.destroy(object).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })
    this.dialogs.close();
  },
  // 进入到swiper image
  intoImage(e) {
    console.log(e.currentTarget.dataset.index)
    this.setData({
      flag: e.currentTarget.dataset.index
    })

  },

  // 移动按钮模块
  // 记录起始位置
  buttonStart: function(e) {
    // console.log(e)
    startPoint = e.touches[0]
  },
  // 按钮移动是判断
  buttonMove: function(e) {
    // console.log(e.touches[0].clientX)
    var endPoint = e.touches[e.touches.length - 1]
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint
    var buttonTop = this.data.buttonTop + translateY
    var buttonLeft = this.data.buttonLeft + translateX
    //判断是移动否超出屏幕
    if (buttonLeft + 50 >= this.data.windowWidth) {
      buttonLeft = this.data.windowWidth - 50;
    }
    if (buttonLeft <= 0) {
      buttonLeft = 0;
    }
    if (buttonTop <= 0) {
      buttonTop = 0
    }
    if (buttonTop + 50 >= this.data.windowHeight) {
      buttonTop = this.data.windowHeight - 50;
    }
    this.setData({
      buttonTop: buttonTop,
      buttonLeft: buttonLeft
    })
  },
  buttonEnd: function(e) {

  },
  // 移动按钮点击事件
  buttonNav(e) {
    if (this.data.currentTab == 0) {
      wx.navigateTo({
        url: '../addAffair/addAffair',
      })
    } else if (this.data.currentTab == 1) {
      wx.navigateTo({
        url: '../birthday/birthday'
      })
    } else if (this.data.currentTab == 2) {
      wx.navigateTo({
        url: '../commemorationDay/commemorationDay'
      })

      this.setData({
        currentIndexs: 0,
        intoIndex:0

      })
    }

  },

  // scolle上下滑动事件
  scroll(e) {

    // console.log(e)
    // for (let i = 0; i < this.data.textareaValueList.length; ++i) {
    //   // if (this.data.textareaValueList[i] === this.data.toView) {
    //   //   this.setData({
    //   //     toView: this.data.textareaValueList[i + 1],
    //   //     scrollTop: (i + 1) * 200
    //   //   })
    //   //   break
    //   // }
    //   this.setData({
    //     toView: this.data.textareaValueList[0],
    //   })
    // }
  },


})